<?php defined('SYSPATH') OR die('No direct access allowed.');

class Index_Controller extends Master_Channel_Controller{

	public function index(){
		$view = new View('channel/ch_index');

		$this->set_title('สถานีเพลง');
		$this->template->content = $view;
		$this->template->render(TRUE);
	}

	public function list_channel(){

		$view = new View('channel/index');

		$this->set_title('สถานีเพลง');

		//get status user
		//		$auth = new Auth;
		//		$user_admin = $auth->logged_in('admin');
		//		$user = $auth->get_user();

		//		if ($user_admin) {
		//			$view = new View('channel/admin_channel');
		//		}

		//		if (!empty($user)) {
		//			Kohana::log("debug","user != null");
		//			Kohana::log("debug",$user->id);
		//			Kohana::log("debug",$user->username);
		//		}

		$channels = new Channels_Model;

		// Instantiate Pagination, passing it the total number of product rows.
		$paging = new Pagination(array
		(
			'base_url'    => 'channel/index/ajax_page/',
			'uri_segment' => 4,
			'total_items' => $channels->count_channels(),
		));

		// Render the page links
		$view->pagination = $paging->render();

		// Display X items per page, from offset = page number
		$view->channels = $channels->loads();

		$this->template->content = $view;
		$this->template->render(TRUE);

	}

	//ajax
	public function ajax_page($pagenum=1)
	{
		Kohana::log("debug","Function ajax_page");
		if(request::is_ajax())
		{
			$this->auto_render=false;
			$ajax = new ISS_Ajax();

			$view = new View('channel/admin_channel');
			$channels = new Channels_Model;

			// Instantiate Pagination, passing it the total number of product rows.
			$paging = new Pagination(array
			(
				'base_url'    => 'channel/admin_channel/ajax_page/',
				'uri_segment' => 4,
				'total_items' => $channels->count_channels(),
			));

			// Render the page links
			$view->pagination = $paging->render();

			// Display X items per page, from offset = page number
			$view->channels = $channels->browse($paging->items_per_page, $paging->sql_offset);

			$selector = "mainCont";
			$content = $view;
			$ajax->addViewUpdate($selector, $content);
			echo json_encode($ajax->getHtmlAjax());
		}
		else {
			$this->page($pagenum);
		}
	}

	public function page($pagenum=1)
	{
		Kohana::log("debug","Function Page");

		//		$auth = new Auth;
		//		$user_role = $auth->logged_in('admin');
		//		Kohana::log('info',$user_role);

		//		if ($user_role) {
		$view = new View('channel/admin_channel');
		//		}else{
		//			$view = new View('channel/index');
		//		}

		$this->set_title('ผู้ดูแลระบบ - สถานีเพลง');
			
		$channels = new Channels_Model;

		// Instantiate Pagination, passing it the total number of product rows.
		$paging = new Pagination(array
		(
			'base_url'    => 'channel/admin_channel/ajax_page/',
			'uri_segment' => 4,
			'total_items' => $channels->count_channels(),
		));

		// Render the page links
		$view->pagination = $paging->render();

		// Display X items per page, from offset = page number
		//$view->channels = $channels->browse($paging->items_per_page, $paging->sql_offset);
		$view->channels = $channels->loads();

		$this->template->content = $view;
		$this->template->render(TRUE);

	}

}

